<?php
/**
 * 厅主管理-客户基本资料
 * User: Tommy
 * Date: 2018/11/9
 * Time: 14:19
 */

namespace App\Services;


use App\Models\Hall;
use App\Models\Money;
use App\Models\MoneyPartner;
use common\enum\MasterLogEnum;
use common\utils\RSACrypt;
use common\utils\SysCode;

class HallBasicService
{
    /**
     * 获取用户基本数据
     * @param int $id
     * @return array
     */
    public static function getValues(int $id)
    {
        $money = Money::where(['hall_id' => $id])->selectRaw("SUM(balance) as balance")->selectRaw("SUM(freeze_withdraw)")->selectRaw("SUM(freeze_exchange)")->first()->toArray();

        $moneyPartner = MoneyPartner::where(['hall_id' => $id])->selectRaw("SUM(balance)")->first()->toArray();
        $balance      = array_sum(array_merge($money, $moneyPartner));

        $select = ['company_name', 'agent_count', 'user_count', 'mobile', 'skype', 'wechat', 'email', 'remark', 'created_at', 'id', 'admin_name', 'emergency', 'emergency_code'];
        $hall   = Hall::select($select)->where(['id' => $id])->first()->toArray();

        $hall['balance']   = !empty($balance) ? $balance : '';
        $hall['mobile']    = !empty($hall['mobile']) ? RSACrypt::rsaDecrypt($hall['mobile']) : '';
        $hall['skype']     = !empty($hall['skype']) ? RSACrypt::rsaDecrypt($hall['skype']) : '';
        $hall['wechat']    = !empty($hall['wechat']) ? RSACrypt::rsaDecrypt($hall['wechat']) : '';
        $hall['email']     = !empty($hall['email']) ? RSACrypt::rsaDecrypt($hall['email']) : '';
        $hall['emergency'] = !empty($hall['emergency']) ? RSACrypt::rsaDecrypt($hall['emergency']) : '';
        $hall['emergency_code'] = !empty($hall['emergency_code']) ? RSACrypt::rsaDecrypt($hall['emergency_code']) : '';
        return $hall;
    }

    /**
     * 修改用户基本数据
     * @param int $id
     * @param array $values
     * @return mixed
     * @throws \App\Exceptions\CustomException
     */
    public static function editBasicData(int $id, array $values)
    {
        if (!empty($values['hall_id'])) {
            unset($values['hall_id']);
        }

        $hallName = Hall::where(['company_name' => trim($values['company_name'])])->whereRaw("id != ".$id)->first();
        if ($hallName) {
            error_exit(SysCode::HALL_BASIC_NAME_ONLY);
        }
        $updateDada = [
            'mobile'         => !empty($values['mobile']) ? RSACrypt::rsaEncrypt($values['mobile']) : '',
            'skype'          => !empty($values['skype']) ? RSACrypt::rsaEncrypt($values['skype']) : '',
            'wechat'         => !empty($values['wechat']) ? RSACrypt::rsaEncrypt($values['wechat']) : '',
            'email'          => !empty($values['email']) ? RSACrypt::rsaEncrypt($values['email']) : '',
            'emergency'      => !empty($values['emergency']) ? RSACrypt::rsaEncrypt($values['emergency']) : '',
            'emergency_code' => !empty($values['emergency_code']) ? RSACrypt::rsaEncrypt($values['emergency_code']) : '',
            'company_name'   => !empty($values['company_name']) ? trim($values['company_name']) : '',
            'remark'         => !empty($values['remark']) ? $values['remark'] : '',
        ];
        $result     = Hall::where(['id' => $id])->update($updateDada);
        if (!$result) {
            error_exit(SysCode::HALL_BASIC_UPDATE_ERROR);
        }
        $hallName = Hall::where(['id' => $id])->first()->toArray();
        $remark   = str_replace('{:name}', $hallName['company_name'] ,MasterLogEnum::REMARK['hall_data_edit']);
        MasterLogService::writeMasterLog(MasterLogEnum::HALL_DATA_EDIT, MasterLogEnum::SUCCESS, $remark);
        return $result;
    }
}